/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateOrderDialog.java
 *
 * Created on 2009-12-6, 1:00:00
 */
package com.ordering.menu;

import com.ordering.dao.TFoodTypeJpaController;
import com.ordering.dao.TOrderDetailJpaController;
import com.ordering.dao.TOrderHeadJpaController;
import com.ordering.dao.exceptions.IllegalOrphanException;
import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.TFoodType;
import com.ordering.pojo.TMenu;
import com.ordering.pojo.TOrderDetail;
import com.ordering.utils.WindowLocationUtils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author sulan
 */
public class CreateOrderDialog extends javax.swing.JDialog {

    /** Creates new form CreateOrderDialog */
    public CreateOrderDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        WindowLocationUtils.setWindowCenter(this);

        //tree
        rootNode = new DefaultMutableTreeNode("菜谱");

        tm = new DefaultTreeModel(rootNode);
        jTree1.setModel(tm);
        this.initTree();
        jTree1.expandPath(new TreePath(((DefaultMutableTreeNode) rootNode).getPath()));
        WindowLocationUtils.hideColumn(orderDetailList, 0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        tOrderHead1 = new com.ordering.pojo.TOrderHead();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        contentPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        foodNameText = new javax.swing.JTextField();
        foodTypeText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        priceText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        foodDesc = new javax.swing.JTextArea();
        photoLabel = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tableNoText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        addressText = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        phoneText = new javax.swing.JTextField();
        btnDelFood = new javax.swing.JButton();
        btnConfirmOrder = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        cardNumText = new javax.swing.JTextField();
        jScrollPane6 = new javax.swing.JScrollPane();
        orderDetailList = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        orderTypeComp = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(750, 600));
        getContentPane().setLayout(null);

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setLastDividerLocation(1000);
        jSplitPane1.setName("jSplitPane1"); // NOI18N
        jSplitPane1.setPreferredSize(new java.awt.Dimension(434, 600));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTree1.setName("jTree1"); // NOI18N
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jTree1);

        jScrollPane1.setViewportView(jScrollPane2);

        jSplitPane1.setLeftComponent(jScrollPane1);

        contentPanel.setName("contentPanel"); // NOI18N

        jLabel1.setText("菜名:");
        jLabel1.setName("jLabel1"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        foodNameText.setEnabled(false);
        foodNameText.setName("foodNameText"); // NOI18N
        foodNameText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                foodNameTextActionPerformed(evt);
            }
        });

        foodTypeText.setEnabled(false);
        foodTypeText.setName("foodTypeText"); // NOI18N
        foodTypeText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                foodTypeTextActionPerformed(evt);
            }
        });

        jLabel2.setText("种类:");
        jLabel2.setName("jLabel2"); // NOI18N

        priceText.setEnabled(false);
        priceText.setName("priceText"); // NOI18N
        priceText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                priceTextActionPerformed(evt);
            }
        });

        jLabel3.setText("单价:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("描述:");
        jLabel4.setName("jLabel4"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        foodDesc.setColumns(20);
        foodDesc.setRows(3);
        foodDesc.setEnabled(false);
        foodDesc.setName("foodDesc"); // NOI18N
        jScrollPane3.setViewportView(foodDesc);

        photoLabel.setText("暂无图片");
        photoLabel.setName("photoLabel"); // NOI18N

        jLabel5.setText("桌号:");
        jLabel5.setName("jLabel5"); // NOI18N

        tableNoText.setName("tableNoText"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tOrderHead1, org.jdesktop.beansbinding.ELProperty.create("${tableNo}"), tableNoText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel6.setText("地址:");
        jLabel6.setName("jLabel6"); // NOI18N

        addressText.setName("addressText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tOrderHead1, org.jdesktop.beansbinding.ELProperty.create("${address}"), addressText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel7.setText("联系电话:");
        jLabel7.setName("jLabel7"); // NOI18N

        phoneText.setName("phoneText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tOrderHead1, org.jdesktop.beansbinding.ELProperty.create("${phone}"), phoneText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        phoneText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                phoneTextActionPerformed(evt);
            }
        });

        btnDelFood.setText("删除菜");
        btnDelFood.setName("btnDelFood"); // NOI18N
        btnDelFood.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelFoodActionPerformed(evt);
            }
        });

        btnConfirmOrder.setText("确认订单");
        btnConfirmOrder.setName("btnConfirmOrder"); // NOI18N
        btnConfirmOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmOrderActionPerformed(evt);
            }
        });

        jLabel8.setText("VIP卡号:");
        jLabel8.setName("jLabel8"); // NOI18N

        cardNumText.setName("cardNumText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tOrderHead1, org.jdesktop.beansbinding.ELProperty.create("${cardNum}"), cardNumText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane6.setName("jScrollPane6"); // NOI18N

        orderDetailList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "菜  名", "种  类", "单  价", "描  述", "数  量"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        orderDetailList.setMaximumSize(new java.awt.Dimension(2147483647, 200));
        orderDetailList.setMinimumSize(new java.awt.Dimension(375, 200));
        orderDetailList.setName("orderDetailList"); // NOI18N
        orderDetailList.setPreferredSize(new java.awt.Dimension(375, 200));
        jScrollPane6.setViewportView(orderDetailList);

        jLabel9.setText("类型:");
        jLabel9.setName("jLabel9"); // NOI18N

        orderTypeComp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "普通", "VIP", "外卖" }));
        orderTypeComp.setName("orderTypeComp"); // NOI18N

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(priceText, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                            .addComponent(foodNameText, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                            .addComponent(foodTypeText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                        .addGap(88, 88, 88)
                        .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addGap(99, 99, 99)
                        .addComponent(btnDelFood)
                        .addGap(27, 27, 27)
                        .addComponent(btnConfirmOrder))
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(contentPanelLayout.createSequentialGroup()
                                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(contentPanelLayout.createSequentialGroup()
                                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(orderTypeComp, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(tableNoText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
                                        .addGap(18, 18, 18)
                                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(cardNumText)
                                            .addComponent(phoneText, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)))
                                    .addComponent(addressText, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)))
                            .addComponent(jLabel9))))
                .addContainerGap(88, Short.MAX_VALUE))
        );

        contentPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cardNumText, orderTypeComp, phoneText, tableNoText});

        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
                            .addComponent(foodNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)
                            .addComponent(foodTypeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(priceText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE))
                    .addComponent(photoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addGap(0, 0, 0)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(65, 65, 65))
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(orderTypeComp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(cardNumText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tableNoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(phoneText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(addressText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDelFood)
                    .addComponent(btnConfirmOrder))
                .addGap(105, 105, 105))
        );

        jSplitPane1.setRightComponent(contentPanel);

        getContentPane().add(jSplitPane1);
        jSplitPane1.setBounds(10, 40, 630, 550);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
        if (evt.getClickCount() == 2) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
            Object o = node.getUserObject();
            if (o instanceof TMenu) {
                TMenu menu = (TMenu) o;
                javax.swing.table.DefaultTableModel orderTableModel = (DefaultTableModel) orderDetailList.getModel();
                String[] rowData = new String[6];
                rowData[0] = String.valueOf(menu.getMenuId());
                rowData[1] = menu.getFoodName();
                rowData[2] = String.valueOf(menu.getFoodTypeOid());
                rowData[3] = String.valueOf(menu.getPrice());
                rowData[4] = menu.getDescription();
                rowData[5] = "1";

                int rowCount = orderTableModel.getRowCount();
                for (int i = 0; i < rowCount; i++) {
                    String seq = orderTableModel.getValueAt(i, 0).toString();
                    if (seq.equals(rowData[0])) {
                        JOptionPane.showMessageDialog(this.getParent(), "选择的菜已经点过，只能修改数量！", "菜已经点过", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }

                orderTableModel.addRow(rowData);
            }
        }
    }//GEN-LAST:event_jTree1MouseClicked

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
        Object o = node.getUserObject();
        if (o instanceof TMenu) {
            TMenu menu = (TMenu) o;
            foodNameText.setText(menu.getFoodName());
            foodTypeText.setText(String.valueOf(menu.getFoodTypeOid()));
            priceText.setText(String.valueOf(menu.getPrice()));
            foodDesc.setText(menu.getDescription());

            String photoName = menu.getPhotoName();


            File file = new File("pic/" + photoName);
            photoLabel.setIcon(new ImageIcon(file.getPath()));
        }
        System.out.println("You selected " + node);
}//GEN-LAST:event_jTree1ValueChanged

    private void foodNameTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foodNameTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_foodNameTextActionPerformed

    private void foodTypeTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foodTypeTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_foodTypeTextActionPerformed

    private void priceTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priceTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_priceTextActionPerformed

    private void phoneTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_phoneTextActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_phoneTextActionPerformed

    private void btnConfirmOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmOrderActionPerformed
        if(!this.createOrder())
        {
            JOptionPane.showMessageDialog(this.getParent(), "产生订单失败", "产生订单失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JOptionPane.showMessageDialog(this.getParent(), "产生订单成功！订单编号为: " + tOrderHead1.getOrderNo(), "产生订单成功", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_btnConfirmOrderActionPerformed

    private void btnDelFoodActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelFoodActionPerformed
        int[] selectedRows = orderDetailList.getSelectedRows();
        dtm = (DefaultTableModel) orderDetailList.getModel();
        for (int i = selectedRows.length-1; i>=0; i--) {
            dtm.removeRow(selectedRows[i]);
        }
    }//GEN-LAST:event_btnDelFoodActionPerformed

    private void initTree() {
        TFoodTypeJpaController jpa = new TFoodTypeJpaController();
        List<TFoodType> tfoodTypeList = jpa.findTFoodTypeEntities();
        for (TFoodType fd : tfoodTypeList) {
            DefaultMutableTreeNode foodTypeNode = new DefaultMutableTreeNode(fd);
            rootNode.add(foodTypeNode);

            for (TMenu menu : fd.getTMenuCollection()) {
                DefaultMutableTreeNode menuNode = new DefaultMutableTreeNode(menu);
                foodTypeNode.add(menuNode);
            }
        }
    }

    private boolean createOrder() {
        try {
            javax.swing.table.DefaultTableModel orderTableModel = (DefaultTableModel) orderDetailList.getModel();
            int rowCount = orderTableModel.getRowCount();
            if (rowCount == 0)
            {
                 JOptionPane.showMessageDialog(this.getParent(),
                                "订单中至少点了一个菜才能下单！", "请先点菜",
                                JOptionPane.ERROR_MESSAGE);
                 return false;
            }
            TOrderHeadJpaController jpa = new TOrderHeadJpaController();
            TOrderDetailJpaController jpaDetail = new TOrderDetailJpaController();

            tOrderHead1.setOrderType("0");
            tOrderHead1.setOrderStatus("100");
            tOrderHead1.setCreatUser("Admin");
            tOrderHead1.setCreateDate(new Date());
            tOrderHead1.setUpdateUser("Admin");
            tOrderHead1.setUpdateDate(new Date());
            jpa.create(tOrderHead1);
            tOrderHead1.setOrderNo("order_" + tOrderHead1.getId());
            jpa.edit(tOrderHead1);
            
            Collection<TOrderDetail> tOrderDetailCollection = new ArrayList<TOrderDetail>();
            for (int i = 0; i < rowCount; i++) {
                TOrderDetail tOrderDetail = new TOrderDetail();
                tOrderDetail.setOrderHeadId(tOrderHead1);
                tOrderDetail.setMenuId(Integer.parseInt(orderTableModel.getValueAt(i, 0).toString()));
                tOrderDetail.setQty(Float.valueOf(orderTableModel.getValueAt(i, 5).toString()));

                jpaDetail.create(tOrderDetail);
            }
            tOrderHead1.setTOrderDetailCollection(tOrderDetailCollection);
            return true;

        } catch (IllegalOrphanException ex) {
            Logger.getLogger(CreateOrderDialog.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(CreateOrderDialog.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (Exception ex) {
            Logger.getLogger(CreateOrderDialog.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public void expandTreeNode(JTree aTree, DefaultMutableTreeNode aNode) {
        if (aNode.isLeaf()) {
            return;
        }
        aTree.expandPath(new TreePath(((DefaultMutableTreeNode) aNode).getPath()));
        int n = aNode.getChildCount();
        for (int i = 0; i < n; i++) {
            expandTreeNode(aTree, (DefaultMutableTreeNode) aNode.getChildAt(i));
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CreateOrderDialog dialog = new CreateOrderDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    DefaultMutableTreeNode rootNode;
    DefaultTreeModel tm;
    DefaultTableModel dtm;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField addressText;
    private javax.swing.JButton btnConfirmOrder;
    private javax.swing.JButton btnDelFood;
    private javax.swing.JTextField cardNumText;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JTextArea foodDesc;
    private javax.swing.JTextField foodNameText;
    private javax.swing.JTextField foodTypeText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTree jTree1;
    private javax.swing.JTable orderDetailList;
    private javax.swing.JComboBox orderTypeComp;
    private javax.swing.JTextField phoneText;
    private javax.swing.JLabel photoLabel;
    private javax.swing.JTextField priceText;
    private com.ordering.pojo.TOrderHead tOrderHead1;
    private javax.swing.JTextField tableNoText;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
